package leetcode.code0682;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public int calPoints(String[] ops) {
		int len = ops.length;
		int[] stack = new int[len];
		int p = 0;
		for (String s : ops) {
			if (s.equals("C")) {
				p--;
			} else if (s.equals("D")) {
				stack[p] = stack[p - 1] << 1;
				p++;
			} else if (s.equals("+")) {
				stack[p] = stack[p - 1] + stack[p - 2];
				p++;
			} else {
				stack[p++] = Integer.valueOf(s);
			}
		}
		int ans = 0;
		for (int i = 0; i < p; i++) {
			ans += stack[i];
		}
		return ans;
	}

	@Override
	public void debug4() {
		HelpDebug.compare(30, this.calPoints(HelpDebug.str2array2("[\"5\",\"2\",\"C\",\"D\",\"+\"]")));
		HelpDebug.compare(27, this.calPoints(HelpDebug.str2array2("[\"5\",\"-2\",\"4\",\"C\",\"D\",\"9\",\"+\",\"+\"]")));
		HelpDebug.compare(1, this.calPoints(HelpDebug.str2array2("[\"1\"]")));
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
	}

}
